<?php
class searchdoc extends Model{

	public function getdocbydeptid($dept_id){
		$sql="select * from receiveddoc r inner join department d ".
"on r.departmentid=d.department_id where r.departmentid='{$dept_id}' group by r.document_no order by".
		" r.receiveddocid desc";
		return $this->db->query($sql);
	}

	public function getsearchlist($history=false){
		$filter="";
		$rfilter="";
		if($_POST["docno"]!=""){
			$filter.=" and document_no='{$_POST["docno"]}' ";
		}
		if($_POST["doctype"]!=""){
			$filter.=" and documenttype_id='{$_POST["doctype"]}' ";
		}
		if($_POST["refno"]!=""){
			$filter.=" and referenceno='{$_POST["refno"]}' ";
		}	 
		if($_POST["receivedfrom"]!=""){
			$filter.=" and fromaddress='{$_POST["receivedfrom"]}' ";
		}
	    if($_POST["addressto"]!=""){
			$filter.=" and addressedto='{$_POST["addressto"]}' ";
		}
	   if($_POST["docdate"]!=""){
	      	$str=explode("/",$_POST["docdate"]);
	    	$sdate=$str[2]."-".$str[0]."-".$str[1];
			$filter.=" and docdate='{$sdate}' ";
		}
	    if($_POST["receivedon"]!=""){
	    	$str=explode("/",$_POST["receivedon"]);
	    	$sdate=$str[2]."-".$str[0]."-".$str[1];
			$rfilter.=" and r.received_date='{$sdate}' ";
		}
		$sql="select r.document_no,d.departmentcode,d.name,r.received_date,r.received_time from receiveddoc r ".
		"inner join department d on r.departmentid=d.department_id ".
		"where r.document_no in (select document_no from document where active_yn='Y' ".$filter.") ".
		$rfilter;
		if($history==false){
		  $sql.=" group by r.document_no order by r.receiveddocid desc";	
		}
		return $this->db->query($sql);
	}

}
?>